﻿Public Class xtf_ABMmoneda

    Dim ulg_row As DataRow
    Dim obj_Moneda As cls_moneda_ = Nothing
    Dim dtt_Cotizacion_ As DataTable


    Public Sub New()
        InitializeComponent()
        AgregarValidaciones()

        'Prepara grid y datasource Cotizacion----------------------------------------------------
        gdv_Cotizacion_.Tag = New Collections.Generic.List(Of cls_cotizacion_)
        dtt_Cotizacion_ = New DataTable
        dtt_Cotizacion_.Columns.Add("Fecha", GetType(String))
        dtt_Cotizacion_.Columns.Add("Valor", GetType(Decimal))
        dtt_Cotizacion_.Columns.Add("objeto", GetType(Object))

        gdc_Cotizacion_.DataSource = dtt_Cotizacion_
        gdc_Cotizacion_.ForceInitialize()
        gdv_Cotizacion_.PopulateColumns()
        gdv_Cotizacion_.Columns("objeto").Visible = False
        '-----------------------------------------------------------------------------------------------

    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            obj_Moneda = New cls_Moneda_
            btn_Registro.Enabled = False
            Me.Show()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef Moneda As cls_moneda_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_Moneda) Then
            obj_Moneda = New cls_Moneda_
        End If
        obj_Moneda = Moneda
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_Moneda.ToString
        txt_ID.Text = obj_Moneda.idMoneda
        txt_Nombre.Text = obj_Moneda.nombre
        txt_Descripcion.Text = obj_Moneda.descripcion
        txt_simbolo.Text = obj_Moneda.simbolo

        'Cargo Cotizacion-------------------------------------------------------------------
        dtt_Cotizacion_.Clear()
        gdv_Cotizacion_.Tag.Clear()
        If Not IsNothing(obj_Moneda.Cotizacion_) Then
            If Not IsNothing(obj_Moneda.Cotizacion_.Items) Then
                For Each Cotizacion As cls_cotizacion_ In obj_Moneda.Cotizacion_.Items
                    Dim Item As DataRow = dtt_Cotizacion_.NewRow
                    Item("Fecha") = Cotizacion.fecha.ToShortDateString
                    Item("Valor") = Cotizacion.valor
                    Item("objeto") = Cotizacion
                    dtt_Cotizacion_.Rows.Add(Item)
                Next
            End If
        End If
        gdv_Cotizacion_.Columns("Fecha").SortOrder = DevExpress.Data.ColumnSortOrder.Descending
        '--------------------------------------------------------------------------------------------

        If obj_Moneda.idmoneda = 1 Then
            If Not UsuarioLOG.esroot Then
                txt_Descripcion.Enabled = False
                lcg_Cotizacion.Enabled = False
            End If
        End If

        btn_Registro.Enabled = True

        obj_Moneda.Cotizacion_ = Nothing

        Me.Show()

    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If dtt_Cotizacion_.Rows.Count < 1 Then
            ErrManagerShow("001022", Me)
            Return False
        End If
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_Moneda.nombre = txt_Nombre.Text
            obj_Moneda.descripcion = txt_Descripcion.Text
            obj_Moneda.simbolo = txt_simbolo.Text

            'Guardo Cotizacion------------------------------------------------------
            If IsNothing(obj_Moneda.Cotizacion_) Then
                obj_Moneda.Cotizacion_ = New cls_cotizacion_
            End If
            If IsNothing(obj_Moneda.Cotizacion_.Items) Then
                obj_Moneda.Cotizacion_.Items = New Collections.Generic.List(Of cls_cotizacion_)
            End If
            obj_Moneda.Cotizacion_.Items = New Collections.Generic.List(Of cls_cotizacion_)
            For Each Elemento As DataRow In dtt_Cotizacion_.Rows
                obj_Moneda.Cotizacion_.Items.Add(Elemento("Objeto"))
            Next
            For Each Elemento As cls_cotizacion_ In gdv_Cotizacion_.Tag
                If Elemento.idcotizacion <> 0 Then
                    Elemento.Eliminar(Elemento.idcotizacion)
                End If
            Next
            '---------------------------------------------------------------------

            If txt_ID.Text = "" Then
                obj_Moneda.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_LISTADOmoneda_).AgregarItem(obj_Moneda)
                End If
                Modificar(obj_Moneda, frm_listado, ulg_row)
            Else
                obj_Moneda.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_LISTADOmoneda_).ModificarItem(obj_Moneda, ulg_row)
                End If
                Modificar(obj_Moneda, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_Moneda.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_Moneda.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

#Region "Cotizaciones"

    Private Sub tsb_ArticuloNuevo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb_CotizacionNuevo.Click
        Dim ABMCotizacion As New xtf_ABMcotizacion
        If ABMCotizacion.ShowDialog = Windows.Forms.DialogResult.OK Then
            gdv_Cotizacion_.BeginDataUpdate()
            Dim Item As DataRow = dtt_Cotizacion_.NewRow
            Item("Fecha") = ABMCotizacion.obj_Cotizacion.fecha.ToShortDateString
            Item("Valor") = ABMCotizacion.obj_Cotizacion.valor
            Item("objeto") = ABMCotizacion.obj_Cotizacion
            dtt_Cotizacion_.Rows.Add(Item)
            gdv_Cotizacion_.EndDataUpdate()
        End If
    End Sub

    Private Sub gridView1_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
        Dim view As DevExpress.XtraGrid.Views.Grid.GridView = CType(sender, DevExpress.XtraGrid.Views.Grid.GridView)
        Dim pt As Point = view.GridControl.PointToClient(Control.MousePosition)
        DoRowDoubleClick(view, pt)
    End Sub

    Public Overridable Sub DoRowDoubleClick(ByVal view As DevExpress.XtraGrid.Views.Grid.GridView, ByVal pt As Point)
        Dim info As DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo = view.CalcHitInfo(pt)
        If info.InRow OrElse info.InRowCell Then
            Dim colCaption As String
            If info.Column Is Nothing Then
                colCaption = "N/A"
            Else
                colCaption = info.Column.GetCaption()
            End If
            tsb_CotizacionModificar.PerformClick()
        End If
    End Sub

    Private Sub tsb_ArticulosModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb_CotizacionModificar.Click
        If gdv_Cotizacion_.SelectedRowsCount > 0 Then
            Dim ABMCotizacion As New xtf_ABMcotizacion
            ABMCotizacion.Modificar(gdv_Cotizacion_.GetDataRow(gdv_Cotizacion_.GetSelectedRows(0))("objeto"))
            If ABMCotizacion.ShowDialog = Windows.Forms.DialogResult.OK Then
                gdv_Cotizacion_.BeginDataUpdate()
                Dim Item As DataRow = gdv_Cotizacion_.GetDataRow(gdv_Cotizacion_.GetSelectedRows(0))
                Item("Fecha") = ABMCotizacion.obj_Cotizacion.fecha.ToShortDateString
                Item("Valor") = ABMCotizacion.obj_Cotizacion.valor
                Item("Objeto") = ABMCotizacion.obj_Cotizacion
                gdv_Cotizacion_.EndDataUpdate()
            End If
        End If
    End Sub

    Private Sub tsb_ArticuloEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb_CotizacionEliminar.Click
        If gdv_Cotizacion_.SelectedRowsCount > 0 Then
            gdv_Cotizacion_.BeginDataUpdate()
            gdv_Cotizacion_.Tag.add(gdv_Cotizacion_.GetDataRow(gdv_Cotizacion_.GetSelectedRows(0))("Objeto"))
            dtt_Cotizacion_.Rows.Remove(gdv_Cotizacion_.GetDataRow(gdv_Cotizacion_.GetSelectedRows(0)))
            gdv_Cotizacion_.EndDataUpdate()
        End If
    End Sub

    Private Sub tsb_articulosBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsb_CotizacionBuscar.Click
        Dim buscar As New xtf_buscardor(gdv_Cotizacion_.DataSource, "Cotizaciones", {"Objeto"})
        If buscar.ShowDialog = Windows.Forms.DialogResult.OK Then
            'gdv_Cotizacion_.SetFocusedValue((buscar.obj_resultado))
            gdv_Cotizacion_.FocusedRowHandle = (gdv_Cotizacion_.GetRowHandle(dtt_Cotizacion_.Rows.IndexOf(buscar.obj_resultado)))
            'gdv_Cotizacion_.SelectRow(gdv_Cotizacion_.GetRowHandle(42))
        End If
    End Sub
#End Region
End Class